package com.sgam.mf.dao.entity;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.sgam.mf.dao.GenericDAOImpl;
import com.sgam.mf.domain.entity.Contracts;
import com.sgam.mf.domain.entity.ContractsList;
import com.sgam.mf.helper.ProxyCurrentUser;

@Repository
public class ContractsListDAOImpl extends GenericDAOImpl<ContractsList> implements ContractsListDAO{
	
	protected final Logger log = Logger.getLogger(getClass());
	
	@SuppressWarnings("unchecked")
	public List<ContractsList> list(ProxyCurrentUser user, int listType) {
		log.debug("***********************************************");
		log.debug("ContractsListDAOImpl - list");
		log.debug(user.toString());
		log.debug(listType);
		log.debug("***********************************************");
		String whereForSecurity = "";
		
		if (user.getUserRoleId() == 0 || user.getUserRoleId() == 1){ //If user is Administrator or Manager
			whereForSecurity = " where 1=1 ";
		}
		
//		if (user.getUserRoleId() == 2){ //If user	Consultant OC
//			whereForSecurity = " where ref_status_fk <> 0 ";
//		}	
		
		if (user.getUserRoleId() == 2){ //If user	Consultant OC
			whereForSecurity = " where respons_consultant_fk = " + user.getUserId() + " ";
		}	
		
//		if (user.getUserRoleId() == 3){ //If user	Personal 
//			whereForSecurity = " where ref_status_fk not in (0 , 1)  and respons_personal_fk = " + user.getUserId() + " ";
//		}		
		
		if (user.getUserRoleId() == 4){ //If user	Consultant OT 
			whereForSecurity = " where respons_consultant_fk = " + user.getUserId() + " ";
		}	
		
		if(listType==1) {
			whereForSecurity = whereForSecurity + " and contract_no != '' ";
		}
		
		List<ContractsList>  result = getCurrentSession().createQuery("from Contracts " + whereForSecurity).list();
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<ContractsList> shortList(ProxyCurrentUser user, int listType) {
		log.debug("***********************************************");
		log.debug("ContractsListDAOImpl - ShortList");
		
		String whereForSecurity = "";
		
		if (user.getUserRoleId() == 0 || user.getUserRoleId() == 1){ //If user is Administrator or Manager
			whereForSecurity = " where 1=1 ";
		}
		
//		if (user.getUserRoleId() == 2){ //If user	Consultant OC
//			whereForSecurity = " where ref_status_fk <> 0 ";
//		}	
		
		if (user.getUserRoleId() == 2){ //If user	Consultant OC
			whereForSecurity = " where respons_consultant_fk = " + user.getUserId() + " ";
		}	
		
		if (user.getUserRoleId() == 3){ //If user	Personal 
			whereForSecurity = " where ref_status_fk not in (0 , 1)  and respons_personal_fk = " + user.getUserId() + " ";
		}		
		
		if (user.getUserRoleId() == 4){ //If user	Consultant OT 
			whereForSecurity = " where respons_consultant_fk = " + user.getUserId() + " ";
		}	
		
		if(listType==1) {
			whereForSecurity = whereForSecurity + " and contract_no != '' ";
		}
		
		//String qr = getCurrentSession().getNamedQuery("shortList").getQueryString()+whereForSecurity;
		
		//log.debug("QR = " + qr);
		
		//List<ContractsList>  result   = getCurrentSession().createSQLQuery(qr).list();
		//List<ContractsList>  result  = getCurrentSession().getNamedQuery("shortList").getQueryString()+whereForSecurity.list();
		
		List<ContractsList>  result = getCurrentSession().createQuery("from ContractsList " + whereForSecurity).list();
		
		return result;
	}
}

	
